// 引入模块
const server = require('express')()
const expressStatic = require('express-static')
const url = require('url')
const ejs = require('ejs')
const mysql = require('mysql')
const fs = require('fs')
const bodyParser = require("body-parser");//使用插件，用于解析/post
let path = 84

// 连接数据库
let sql = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"123456",
    database:"interview",
    timezone:"08:00"
})
sql.connect()

// 返回主页
server.get('/',(request,response)=>{
    fs.readFile("static/home.html",(error,data)=>{
        if(error){
           console.log("读取文件失败")
           response.end("error")
        }
        else{
            console.log("读取文件成功")
            response.end(data)
        }
   })
})
// 注册
server.get('/enrollUser',(request,response)=>{
    let userName = url.parse(request.url,true).query.userName
    let password = url.parse(request.url,true).query.password
    sql.query(`INSERT INTO user (userName,password) VALUES ("${userName}","${password}")`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log(`${userName}注册成功`)
            response.end(`${userName}注册成功,请登录`)
        }
    })
})
// 登录
server.get('/userRegister',(request,response)=>{
    let userName = url.parse(request.url,true).query.userName
    let password = url.parse(request.url,true).query.password
    sql.query(`SELECT * FROM user WHERE userName="${userName}" AND password="${password}"`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            if(data.length){
                console.log(`${userName}登录成功`)
                response.end(`${userName}登录成功`)
            }
            else{
                console.log(`${userName}登录失败`)
                response.end(`${userName}登录失败,请检查用户名或密码输入`)
            }
        }
    })
})
// 主页面经
server.get('/getData',(request,response)=>{
    let way = url.parse(request.url,true).query.way
    sql.query(`SELECT * FROM homeexp ORDER BY ${way} DESC`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log("读取homeexp表成功")
            response.end(JSON.stringify(data))
        }
    })
})
// 面经合集
server.get('/getExp',(request,response)=>{
    sql.query(`SELECT * FROM expall`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log("读取expall表成功")
            response.end(JSON.stringify(data))
        }
    })
})
// 公司面经
server.get('/getCompany',(request,response)=>{
    sql.query(`SELECT * FROM company`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log("读取company表成功")
            response.end(JSON.stringify(data))
        }
    })
})
// list
server.get('/getList',(request,response)=>{
    sql.query(`SELECT * FROM list`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log("读取list表成功")
            response.end(JSON.stringify(data))
        }
    })
})
// 个人中心初始化
server.get('/getMe',(request,response)=>{
    let name = url.parse(request.url,true).query.name
    sql.query(`SELECT * FROM user WHERE userName = "${name}"`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log("读取user表成功")
            response.end(JSON.stringify(data))
        }
    })
})
// 修改个人信息
server.get('/reviseInfo',(request,response)=>{
    let userName = url.parse(request.url,true).query.userName
    let name = url.parse(request.url,true).query.name
    let sex = url.parse(request.url,true).query.sex
    let intro = url.parse(request.url,true).query.intro
    let city = url.parse(request.url,true).query.city
    let work = url.parse(request.url,true).query.work
    let edu = url.parse(request.url,true).query.edu
    sql.query(`UPDATE user SET sex="${sex}",intro="${intro}",city="${city}",work="${work}",edu="${edu}" WHERE userName="${userName}"`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log(`修改个人信息成功`)
            response.end(`修改个人信息成功`)
        }
    })
})
// 发布面经
server.get('/writeExp',(request,response)=>{
    let userName = url.parse(request.url,true).query.userName
    let com = url.parse(request.url,true).query.com
    let post = url.parse(request.url,true).query.post
    let title = url.parse(request.url,true).query.title
    let txt = url.parse(request.url,true).query.txt
    let times = url.parse(request.url,true).query.time

    sql.query(`INSERT INTO myexp (username,com,post,title,txt,likes,collect,times) VALUES ("${userName}","${com}","${post}","${title}","${txt}","0","0","${times}")`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log(`发布面经成功`)
            response.end(`发布面经成功`)
        }
    })
})
// 个人中心我的收藏初始化
server.get('/getMyExp',(request,response)=>{
    let way = url.parse(request.url,true).query.way
    let username = url.parse(request.url,true).query.username
    sql.query(`SELECT * FROM myexp WHERE username="${username}" ORDER BY ${way} DESC`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log("读取myexp表成功")
            response.end(JSON.stringify(data))
        }
    })
})
// 删除按钮 我的面经
server.get('/deleteMyExp',(request,response)=>{
    let id = url.parse(request.url,true).query.id
    sql.query(`DELETE FROM myexp WHERE id="${id}"`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log("myexp删除数据成功")
            response.end("myexp删除数据成功")
        }
    })
})
// 编辑我的面经内容
server.use(bodyParser.urlencoded({extended: false}));//解析字符
server.post('/reviseMyExp',(request,response)=>{
    let id = request.body.id
    let txt = request.body.txt
    sql.query(`UPDATE myexp SET txt="${txt}" WHERE id="${id}"`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log(`修改我的面经成功`)
            response.end(`修改我的面经成功`)
        }
    })
})
// 个人中心获取 我的收藏
server.get('/getMyCollect',(request,response)=>{
    let username = url.parse(request.url,true).query.username
    sql.query(`SELECT * FROM mycollect WHERE username="${username}"`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log("读取mycollect表成功")
            response.end(JSON.stringify(data))
        }
    })
})
// list 点赞加一
server.get('/addListLike',(request,response)=>{
    let likes = url.parse(request.url,true).query.likes
    let id = url.parse(request.url,true).query.id
    sql.query(`UPDATE list SET likes="${likes}" WHERE id="${id}"`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log(`点赞`)
            response.end(`点赞`)
        }
    })
})
// list 加入收藏
server.get('/addListCollect',(request,response)=>{
    let username = url.parse(request.url,true).query.username
    let company = url.parse(request.url,true).query.company
    let post = url.parse(request.url,true).query.post
    let title = url.parse(request.url,true).query.title
    let user = url.parse(request.url,true).query.user
    let time = url.parse(request.url,true).query.time
    let likes = url.parse(request.url,true).query.likes
    let collect = url.parse(request.url,true).query.collect
    let photo = url.parse(request.url,true).query.photo
    let txt = url.parse(request.url,true).query.txt
    console.log(company)
    sql.query(`INSERT INTO mycollect (com,post,title,txt,likes,collect,times,name,headphoto,username) VALUES ("${company}","${post}","${title}","${txt}","${likes}","${collect}","${time}","${user}","${photo}","${username}")`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log(`收藏`)
            response.end(`收藏`)
        }
    })
})
// deleteListCollect 删除收藏
server.get('/deleteListCollect',(request,response)=>{
    let title = url.parse(request.url,true).query.title
    sql.query(`DELETE FROM mycollect WHERE title="${title}"`,(error,data)=>{
        if(error){
            console.log(error)
            response.end("error")
        }
        else{
            console.log("mycollect删除数据成功")
            response.end("mycollect删除数据成功")
        }
    })
})



// 拦截静态请求,到static文件中查找
server.use(expressStatic(__dirname + '/static'))

// 监听的端口号
server.listen(path)
console.log(`server is running at ${path}`)
